package com.ease.dao.statement.factory;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;

import com.ease.dao.impl.JdbcTemplateDataAccessObjectImpl;

/**
 * 
 * @author nathanleewei
 * 
 */
public class DatabaseFreeMarkerNamedStatementFactory extends JdbcTemplateDataAccessObjectImpl implements NamedStatementFactory {

	private static final String NAME_COLUMN_NAME_ALIAS = "NAME";

	private static final String NAMEDSTATEMENT_COLUMN_NAME_ALIAS = "NAMEDSTATEMENT";

	//
	private String namedStatementTable = "NAMEDSTATEMENTTABLE";

	private String nameColumnName = NAME_COLUMN_NAME_ALIAS;

	private String namedStatementColumnName = NAMEDSTATEMENT_COLUMN_NAME_ALIAS;

	//
	private Map<String, String> namedStatementMap;

	public String getNamedStatement(String namedStatementName, Object parameter) {
		if (null == namedStatementMap) {
			namedStatementMap = new HashMap<String, String>();
			init();
		}
		return (String) namedStatementMap.get(namedStatementName);
	}

	public void init() {

		String namedStatement = "SELECT " + nameColumnName + " " + NAME_COLUMN_NAME_ALIAS + "," + namedStatementColumnName + " " + NAMEDSTATEMENT_COLUMN_NAME_ALIAS + " FROM " + namedStatementTable;

		this.find(namedStatement, new RowMapper<Object>() {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				namedStatementMap.put(rs.getString(NAME_COLUMN_NAME_ALIAS), rs.getString(NAMEDSTATEMENT_COLUMN_NAME_ALIAS));
				return null;
			}

		});
	}

	/**
	 * @return Returns the namedStatementMap.
	 */
	public Map<String, String> getNamedStatementMap() {
		return namedStatementMap;
	}

	/**
	 * @param namedStatementMap
	 *            The namedStatementMap to set.
	 */
	public void setNamedStatementMap(Map<String, String> namedStatementMap) {
		this.namedStatementMap = namedStatementMap;
	}

	/**
	 * @return Returns the nameColumnName.
	 */
	public String getNameColumnName() {
		return nameColumnName;
	}

	/**
	 * @param nameColumnName
	 *            The nameColumnName to set.
	 */
	public void setNameColumnName(String nameColumnName) {
		this.nameColumnName = nameColumnName;
	}

	/**
	 * @return Returns the namedStatementColumnName.
	 */
	public String getNamedStatementColumnName() {
		return namedStatementColumnName;
	}

	/**
	 * @param namedStatementColumnName
	 *            The namedStatementColumnName to set.
	 */
	public void setNamedStatementColumnName(String namedStatementColumnName) {
		this.namedStatementColumnName = namedStatementColumnName;
	}

	/**
	 * @return Returns the namedStatementTable.
	 */
	public String getNamedStatementTable() {
		return namedStatementTable;
	}

	/**
	 * @param namedStatementTable
	 *            The namedStatementTable to set.
	 */
	public void setNamedStatementTable(String namedStatementTable) {
		this.namedStatementTable = namedStatementTable;
	}

}
